package com.example.demo.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;

/*
全局异常处理
 */
@ControllerAdvice(annotations = {RestController.class,Controller.class})
@ResponseBody
@Slf4j
public class GlobaExceptioonHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public JsonResult<String> exceptionHander(SQLIntegrityConstraintViolationException ex){
        String[] s = ex.getMessage().split(" ");
        log.info(ex.getMessage());
        return new JsonResult(s[2],404);
    }
    @ExceptionHandler(ServiceException.class)
    public JsonResult<String> exceptionHander(ServiceException ex){
        log.info("123456"+ex.getMessage());
        return new JsonResult<>(ex.getMessage(), 500);
    }
}
